package com.example.tse.rbhttpservice.service.moments;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsHomeFollowRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsHomeFollowResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsHomeGetFollowedList;
import com.example.tse.rbhttpservice.service.moments.model.MomentsHomeGetFollowedListRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsHomeGetFollowedListResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsHomeGetHomepageRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsHomeGetHomepageResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsHomeGetRecommendList;
import com.example.tse.rbhttpservice.service.moments.model.MomentsHomeGetRecommendListRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsHomeGetRecommendListResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsHomeGetRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsHomeGetResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;


/**
 * 
 * @author wliu
 *    edi  tse
 */
public class MomentsHomeServiceImpl implements MomentsHomeService {
	private Context context;
	private static Gson gson = new Gson();
	
	
	public MomentsHomeServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 关注功能，点击多次切换状态，关注按钮在此点击都可以取消
	 * @param request
	 * @param listener
	 * @return MomentsHomeFollowResponse
	 */		
	 
	    public void follow(MomentsHomeFollowRequest request, final RemoteServiceListenerT<MomentsHomeFollowResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("memberId",request.getMemberId());

        final String url = "moments/home/follow";


            new HttpService().post(context, url, params, MomentsHomeFollowResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsHomeFollowResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsHomeFollowResponse() : (MomentsHomeFollowResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsHomeFollowResponse() : (MomentsHomeFollowResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * ［首页］：版块列表、达人推荐、帖子列表
［首页］版块列表：妈妈关心等［后台会维护］，只在首页显示版块，一般推荐2个版块，一个版块下显示2个活动
［首页］达人推荐：固定三条推荐的，默认最新添加的三条记录
［首页］帖子列表：同帖子列表
	 * @param request
	 * @param listener
	 * @return MomentsHomeGetResponse
	 */		
	 
	    public void get(MomentsHomeGetRequest request, final RemoteServiceListenerT<MomentsHomeGetResponse> listener) {

        final HttpParams params = new HttpParams();
        


        final String url = "moments/home/get";


            new HttpService().get(context, url, params, MomentsHomeGetResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsHomeGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsHomeGetResponse() : (MomentsHomeGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsHomeGetResponse() : (MomentsHomeGetResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 获取个人主页数据，默认获取当前用户的主页
	 * @param request
	 * @param listener
	 * @return MomentsHomeGetHomepageResponse
	 */		
	 
	    public void getHomepage(MomentsHomeGetHomepageRequest request, final RemoteServiceListenerT<MomentsHomeGetHomepageResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("memberId",request.getMemberId());

        final String url = "moments/home/getHomepage";


            new HttpService().get(context, url, params, MomentsHomeGetHomepageResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsHomeGetHomepageResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsHomeGetHomepageResponse() : (MomentsHomeGetHomepageResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsHomeGetHomepageResponse() : (MomentsHomeGetHomepageResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 点击首页，进入关注界面，获取关注用户的帖子列表（响应参数同帖子详情接口）
	 * @param request
	 * @param listener
	 * @return MomentsHomeGetFollowedListResponse
	 */		
	 
	    public void getFollowedList(MomentsHomeGetFollowedListRequest request, final RemoteServiceListenerT<MomentsHomeGetFollowedListResponse> listener) {

        final HttpParams params = new HttpParams();
        


        final String url = "moments/home/getFollowedList";


        
        /**
        home
        getFollowedList
        GetFollowedList
        点击首页，进入关注界面，获取关注用户的帖子列表（响应参数同帖子详情接口）
        GET
        MomentsHome
        MomentsHomeGetFollowedList
        moments/home/getFollowedList
        array
        */
	
           final Type type = new TypeToken<List<MomentsHomeGetFollowedList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MomentsHomeGetFollowedListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MomentsHomeGetFollowedList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MomentsHomeGetFollowedListResponse responce = new MomentsHomeGetFollowedListResponse();
                 responce.setGetFollowedList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MomentsHomeGetFollowedListResponse());
                 } else {
                     List<MomentsHomeGetFollowedList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsHomeGetFollowedListResponse responce = new MomentsHomeGetFollowedListResponse();
                     responce.setGetFollowedList(items);
                     listener.okData(rudderResponse == null ? new MomentsHomeGetFollowedListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MomentsHomeGetFollowedListResponse(), fromWhere);
                 } else {
                     List<MomentsHomeGetFollowedList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsHomeGetFollowedListResponse responce = new MomentsHomeGetFollowedListResponse();
                     responce.setGetFollowedList(items);
                     listener.okData(rudderResponse == null ? new MomentsHomeGetFollowedListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }
         });
    }            
        
        

   
	
	
	/**
	 * 点击首页的达人推荐进入，显示所有推荐给当前用户达人列表，对于所有用户推荐数据都是相同
	 * @param request
	 * @param listener
	 * @return MomentsHomeGetRecommendListResponse
	 */		
	 
	    public void getRecommendList(MomentsHomeGetRecommendListRequest request, final RemoteServiceListenerT<MomentsHomeGetRecommendListResponse> listener) {

        final HttpParams params = new HttpParams();
        


        final String url = "moments/home/getRecommendList";


        
        /**
        home
        getRecommendList
        GetRecommendList
        点击首页的达人推荐进入，显示所有推荐给当前用户达人列表，对于所有用户推荐数据都是相同
        GET
        MomentsHome
        MomentsHomeGetRecommendList
        moments/home/getRecommendList
        array
        */
	
           final Type type = new TypeToken<List<MomentsHomeGetRecommendList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MomentsHomeGetRecommendListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MomentsHomeGetRecommendList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MomentsHomeGetRecommendListResponse responce = new MomentsHomeGetRecommendListResponse();
                 responce.setGetRecommendList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MomentsHomeGetRecommendListResponse());
                 } else {
                     List<MomentsHomeGetRecommendList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsHomeGetRecommendListResponse responce = new MomentsHomeGetRecommendListResponse();
                     responce.setGetRecommendList(items);
                     listener.okData(rudderResponse == null ? new MomentsHomeGetRecommendListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MomentsHomeGetRecommendListResponse(), fromWhere);
                 } else {
                     List<MomentsHomeGetRecommendList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsHomeGetRecommendListResponse responce = new MomentsHomeGetRecommendListResponse();
                     responce.setGetRecommendList(items);
                     listener.okData(rudderResponse == null ? new MomentsHomeGetRecommendListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }
         });
    }            
        
        

   
	
	
}